# @Time: 2023/10/8 17:09
# @Auther: MHC
# @File: weather.py
# @Description: 获取当前的时间信息和天气信息

import requests
import json
import datetime
import time


# 获取当前日期
def get_current_date():
    current_date = datetime.date.today()
    # print("当前日期:", current_date)
    return current_date


# 获取当前星期（0 代表星期一，6 代表星期日）
def get_current_weekday():
    current_date = datetime.date.today()
    current_weekday = current_date.weekday()
    if current_weekday == 0:
        # print("当前星期:星期一")
        return "星期一"
    elif current_weekday == 1:
        # print("当前星期:星期二")
        return "星期二"
    elif current_weekday == 2:
        # print("当前星期:星期三")
        return "星期三"
    elif current_weekday == 3:
        # print("当前星期:星期四")
        return "星期四"
    elif current_weekday == 4:
        # print("当前星期:星期五")
        return "星期五"
    elif current_weekday == 5:
        # print("当前星期:星期六")
        return "星期六"
    elif current_weekday == 6:
        # print("当前星期:星期日")
        return "星期日"


# 获取当前时间
def get_current_time():
    current_time = time.strftime("%H:%M:%S", time.localtime())
    # print("当前时间:", current_time)
    return current_time


# 使用高德天气API获取当前天气(暂时以武汉为例，其余城市查看高德天气API文档城市编码即可)
def get_current_weather():
    # 下方使用您自己的token
    key = "xxxxxxxxxxxxxxxxxxxxxxxx"
    # 武汉
    city = '420100'
    url = "https://restapi.amap.com/v3/weather/weatherInfo?city=" + city + "&key=" + key
    response = requests.get(url)
    response.encoding = 'utf-8'
    data = json.loads(response.text)
    # 检查是否获取成功
    if data['status'] == '1':
        # 获取温度
        temperature = data['lives'][0]['temperature_float']
        # print("当前温度:", temperature)
        # 获取湿度
        humidity = data['lives'][0]['humidity_float']
        # print("当前湿度:", humidity)
        # 获取天气信息
        weather = data['lives'][0]['weather']
        # print("当前天气:", weather)
        info = str(temperature) + "-" + str(humidity) + "-" + weather
        # print("天气信息:", info)
        return info
    else:
        print("获取天气信息失败")
        return "获取天气信息失败"


if __name__ == '__main__':
    get_current_date()
    get_current_weekday()
    get_current_time()
    get_current_weather()



